package com.example.demo2.util;

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

    private static final Properties props;

    static {
        try {
            InputStream resourse =
                    DBUtil.class.getClassLoader()
                            .getResourceAsStream("db.properties");
            props = new Properties();
            props.load(resourse);
            Class.forName("com.mysql.cj.jdbc.Driver");//加载mysql的驱动
        } catch (IOException e) {
            throw new RuntimeException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection(){
        try {
            return DriverManager.getConnection(
                    props.getProperty("jdbcUrl"),
                    props.getProperty("username"),
                    props.getProperty("password"));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
